//
//  ViewController.m
//  11205 Sections
//
//  Created by student on 16/3/3.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "ViewController.h"

static NSString *identifier=@"Cell";

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchDisplayDelegate>

@property(nonatomic,strong)NSDictionary *namesDict;
@property(nonatomic,strong)NSArray *keys;


//搜索控制器
@property(nonatomic,strong)UISearchDisplayController *searchController;
@property(nonatomic,strong)NSMutableArray *filterNames;

@property(weak,nonatomic)IBOutlet UITableView *tableView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *path=[[NSBundle mainBundle] pathForResource:@"sortednames" ofType:@"plist"];
    self.namesDict=[NSDictionary dictionaryWithContentsOfFile:path];
    self.keys=[[self.namesDict allKeys] sortedArrayUsingSelector:@selector(compare:)];
    
    //注册单元格模板
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:identifier];
    //添加搜索框
    UISearchBar *searchBar=[[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 375, 44)];
    self.tableView.tableHeaderView=searchBar;
    //创建搜索控制器
    self.searchController=[[UISearchDisplayController alloc]initWithSearchBar:searchBar contentsController:self];
    self.searchController.searchResultsDataSource=self;
    self.searchController.searchResultsDelegate=self;
    self.searchController.searchResultsTableView.tag=1;
    [self.searchController.searchResultsTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:identifier];
   
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (tableView.tag==1)
    {
        return 1;
    }
    else{
        return self.keys.count;
    }
    
   }

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView.tag==1)
    {
        return self.filterNames.count;
    }
    else
    {
    NSString *key=self.keys[section];
    NSArray *arr=self.namesDict[key];
    return arr.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if(tableView.tag == 1)
    {
        cell.textLabel.text = self.filterNames[indexPath.row];
        return cell;
    }
    else
    {
        NSString *key = self.keys[indexPath.section];
        NSArray *arr = self.namesDict[key];
        cell.textLabel.text = arr[indexPath.row];
        return cell;
    }
}

// 段头文字
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if(tableView.tag == 1)
    {
        return nil;
    }
    else
    {
        NSString *key = self.keys[section];
        return key;
    }
}

// 右侧导航
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    if(tableView.tag == 1)
    {
        return nil;
    }
    else
    {
        return self.keys;
    }
}


#pragma mark-搜索控制器的事件处理
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    if (searchString.length>0) {
        self.filterNames=[NSMutableArray array];
        //定义一个谓词对象
        NSPredicate *pre=[NSPredicate predicateWithFormat:@"self contains %@",searchString];
        for(NSString *key in self.keys)
        {
            NSArray *names = self.namesDict[key];
            NSArray *tmp = [names filteredArrayUsingPredicate:pre];// 使用谓词对象过滤数组
            
            [self.filterNames addObjectsFromArray:tmp];
        }
    }
    return YES;
}

@end
